package com.shuoyue.ycgk.ui.account;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.utils.EditCheckUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ResetPwdActivity extends BaseMvpActivity<AccountContract.Presenter>
        implements AccountContract.View, SmsContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.pass_code)
    EditText passCode;
    @BindView(R.id.get_code)
    Button getCode;
    @BindView(R.id.password1)
    EditText password1;
    @BindView(R.id.password2)
    EditText password2;
    SmsContract.Presenter smsPresenter;
    private TimeCount time;

    String indexPhoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initView() {
        pageTitle.setText("重置密码");
        time = new TimeCount(60000, 1000);
        UserInfo userInfo = SPUtils.getUserInfo(mContext);
        if (userInfo != null) {
            indexPhoneNum = userInfo.getPhone();
            if (!TextUtils.isEmpty(indexPhoneNum)) {
                phoneNum.setText(indexPhoneNum);
            }
        }
    }

    @Override
    public void initData() {
        mPresenter = new AccountContract.Presenter();
        mPresenter.attachView(this);
        smsPresenter = new SmsContract.Presenter();
        smsPresenter.attachView(this);
    }

    @Override
    public void loginSuc(UserInfo userInfo) {

    }

    @Override
    public void registSuc() {

    }

    @Override
    public void registFail(String msg) {

    }

    @Override
    public void resetSuc() {
        toast("密码已修改");
        MemeryCatch.token = "";
        SPUtils.clearUserPassword(mContext);
        SPUtils.clearUserInfo(mContext);
        finish();
    }

    @Override
    public void bindPhone(String openId, String accessToken) {

    }

    @OnClick({R.id.back, R.id.get_code, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.get_code:
                getVCode();
                break;
            case R.id.submit:
                submit();
                break;
        }
    }

    void getVCode() {
        String phone = phoneNum.getText().toString();
        if (!EditCheckUtil.isTelPhoneNumber(phone)) {
            toast("请输入正确的手机号码");
            return;
        }


        smsPresenter.sendSms(phone);
    }

    void submit() {
        String phone = phoneNum.getText().toString();
        String code = passCode.getText().toString();
        String pwd1 = password1.getText().toString();
        String pwd2 = password2.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            toast("请输入手机号码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            toast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)) {
            toast("请输入密码");
            return;
        }
        if (!pwd1.equals(pwd2)) {
            toast("密码不一致");
            return;
        }
        if (pwd1.length() < 8) {
            toast("密码长度需大于等于8位");
            return;
        }
        if(!pwd1.matches("^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[\\W].*))[\\W0-9A-Za-z]{8,16}$")){
            toast("必须包含数字、英文字母、特殊符号");
            return;
        }

        mPresenter.reset(phone, pwd1, code);
    }

    /**
     * 发送验证码后
     */
    @Override
    public void sendSuc(String phone) {
        time.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (time != null) {
            time.cancel();
        }
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.themeColor));
            getCode.setEnabled(false);
            getCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
            getCode.setEnabled(true);
            getCode.setText("重新获取");
        }
    }
}
